package com.hitqz.robot.camera.lib.callback;

import com.hitqz.robot.camera.common.Constant;
import com.hitqz.robot.camera.common.SavePath;
import com.hitqz.robot.camera.lib.NetSDKLib;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

@Slf4j
public class fCaptureReceiveCB implements NetSDKLib.fSnapRev {
    BufferedImage bufferedImage = null;

    @Override
    public void invoke(NetSDKLib.LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {
        if (pBuf != null && RevLen > 0) {
            String strFileName = SavePath.getSavePath().getSaveCapturePath();

            log.info("strFileName = " + strFileName);
            byte[] buf = pBuf.getByteArray(0, RevLen);
            ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
            try {
                bufferedImage = ImageIO.read(byteArrInput);
                if (bufferedImage == null) {
                    return;
                }
                log.info("loginId:{}，cmdserial:{}" , lLoginID.longValue(),CmdSerial);
                Constant.ROBOT_DH_PICTURE_URL.put(lLoginID.longValue()+"_"+CmdSerial, strFileName);
                ImageIO.write(bufferedImage, "jpg", new File(strFileName));
//                Lock lock = Constant.ROBOT_DH_TAKE_PICTURE_LOCK.get(lLoginID.longValue());
//                if (lock!=null){
//                    synchronized (lock) {
//                        lock.notifyAll();
//                    }
//                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
